package cn.vc.content.service.article;

import cn.vc.content.entity.article.Article;
import cn.vc.content.entity.video.dto.VideoDto;
import cn.vc.content.entity.video.po.Video;
import cn.vc.content.mapper.article.ArticleMapper;
import cn.vc.media.entity.po.MediaFiles;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ArticleService {
    private final ArticleMapper articleMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private final HttpServletRequest request;

    @Autowired
    public ArticleService(ArticleMapper articleMapper, HttpServletRequest request) {
        this.articleMapper = articleMapper;
        this.request = request;
    }


    /**
     * 发布视频
     * @param article
     * @return
     */
    public boolean add(Article article) {
        if (article==null) return false;
        article.setId(null);
        return articleMapper.insert(article)==1;
    }

    /**
     * 审核通过
     */
    public int passTheAudit(Long id) {
        Article article = articleMapper.selectById(id);
        article.setIsDraft(false);
        return articleMapper.updateById(article);
    }


    /**
     * 添加点击数
     * @param articleId
     */
    public void tap(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setTapNum(article.getTapNum()+1);
        articleMapper.updateById(article);
    }
    /**
     * 点赞
     * @param articleId
     */
    public void like(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setLikeNum(article.getLikeNum()+1);
        articleMapper.updateById(article);
    }
    /**
     * 取赞
     * @param articleId
     */
    public void noLike(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setLikeNum(article.getLikeNum()-1);
        articleMapper.updateById(article);
    }
    /**
     * 投币
     * @param articleId
     */
    public void coins(Long articleId, int coins) {
        Article article = articleMapper.selectById(articleId);
        article.setCoinsNum(article.getCoinsNum()+coins);
        articleMapper.updateById(article);
    }
    /**
     * 收藏
     * @param articleId
     */
    public void collect(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setCollectNum(article.getCollectNum()+1);
        articleMapper.updateById(article);
    }
    /**
     * 取消收藏
     * @param articleId
     */
    public void noCollect(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setCollectNum(article.getCollectNum()<=0?0:article.getCollectNum()-1);
        articleMapper.updateById(article);
    }
    /**
     *  对指定ID视频评论
     * @param articleId
     */
    public void review(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setReviewNum(article.getReviewNum()+1);
        articleMapper.updateById(article);
    }


    /**
     * 查询需要审核的视频
     * @return
     */
    public List<Article> selectAllByIsDraft() {
        // 查询所有需要审核的视频信息
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Article::getIsDraft, true);
        // 分页查询，只查询前十条
        Page<Article> articlePage = new Page<>(1, 10);
        IPage<Article> articleIPage = articleMapper.selectPage(articlePage, lqw);
        List<Article> articleList = articleIPage.getRecords();
        return articleList;
    }
    /**
     * 查询审核通过的视频
     * @return
     */
    public List<Article> selectAllByNotDraft() {
        List<Article> articleList = JSON.parseArray(stringRedisTemplate.opsForValue().get("article/all_by_notdraft"), Article.class);
        // 没有则去数据库查询
        if (articleList==null) {
            // 查询所有视频信息
            LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Article::getIsDraft, false);
            articleList = articleMapper.selectList(lqw);
            stringRedisTemplate.opsForValue().set("article/all_by_notdraft", JSON.toJSONString(articleList), 30, TimeUnit.SECONDS);
        }
        return articleList;
    }
    /**
     * 查询指定ID文章
     * @param id
     * @return
     */
    public Article selectById(Long id) {
        return articleMapper.selectById(id);
    }
    public List<Article> selectByIds(List<Long> ids) {
        List<Article> articleList = new ArrayList<>();
        ids.forEach(id -> {
            articleList.add(this.selectById(id));
        });
        return articleList;
    }
}
